import {
  openAuthMiniApp,
  disposeAuthData
} from "dingtalk-design-libs/biz/openAuthMiniApp";

Page({
  data: {
    userid: "",
    name: "",
    mobile: "",
    roles: "",

    data: ""
  },
  /**
   * 通过openAuthMiniApp唤起授权套件
   */
  onTap() {
    let _this = this;
    // return openAuthMiniApp({
    //   path: "pages/home/home", //不要改！！！ 这里是小程序dingwlanwvdmrtjjwdmd下的一个页面地址
    //   panelHeight: "percent50",
    //   extraData: {
    //     clientId: "ding0etja9mayg7zjvog", // 应用ID(唯一标识)
    //     rpcScope: "Contact.User.Read",
    //     fieldScope: "Contact.User.mobile",
    //     type: 0,
    //     ext: JSON.stringify({}),
    //     from: ""
    //   }
    // });
    dd.getAuthCode({
      success: res => {
        _this.setData({
          ddd: res.authCode
        });
        _this.huoqu(res.authCode);
      }
    });
  },
  huoqu(code) {
    let _this = this;

    dd.httpRequest({
      //根据appKey和appsecret来获取Token
      url:
        "https://oapi.dingtalk.com/gettoken?appkey=ding0etja9mayg7zjvog&appsecret=nPH7Ag8WnYDqOa3ZwEMrkthJhsQo0SjzaSHoJjJ5QTLr4CWThOfIMweMH47obWbO",
      success: function(res) {
        console.log("res：", res);

        var access_token = res.data.access_token;

        console.log(code);
        console.log("access_token：", access_token);

        dd.httpRequest({
          url:
            "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?access_token=" +
            access_token +
            "&code=" +
            code,
          method: "POST",

          success: function(res) {
            console.log("获取得到的用户信息：", res);

            dd.httpRequest({
              url:
                "https://oapi.dingtalk.com/user/get?access_token=" +
                access_token +
                "&userid=" +
                res.data.result.userid,
              success: function(res) {
                console.log("根据访问Token和用户id得到用户详细信息：", res);
                console.log("用户Id：", res.data.userid);
                console.log("用户名", res.data.name);
                console.log("电话", res.data.mobile);
                console.log("errmsg:", res.data.errmsg);
                console.log("角色权限：", res.data.roles);
                _this.setData({
                  userid: res.data.userid,
                  name: res.data.name,
                  mobile: res.data.mobile,
                  roles: res.data.roles[0].name
                });
              }
            });
          }
        });
      },
      fail: err => {
        _this.setData({
          err: JSON.stringify(err)
        });
      }
    });
  },
  saoma() {
    let _this = this;
    dd.scan({
      type: "qr",
      success: res => {
        // console.log(res);
        dd.showToast(
          {
            type: "success",
            content: "操作成功",
            duration: 1000,
            success: () => {
              // dd.alert({
              //   title: "toast 消失了"
              // });
            }
          },() => {
            _this.setData({
              data: res.code
            });
          }
        );
      }
    });
  },
  onShow(e) {
    let _this = this;
    this.onTap();
    // disposeAuthData(options => {
    //   console.log("options", options);
    //   _this.huoqu(options.result.authCode);
    //   _this.setData({
    //     ddd: JSON.stringify(options)
    //   });
    //   dd.alert({
    //     title: "disposeAuthData",
    //     content: JSON.stringify(options)
    //   });
    // });
  },

  onLoad() {}
});
